#include "UUOO.h"
#include <jni.h>
#include <android/log.h>

#ifndef LOG_H__
#define LOG_TAG "cpp-log"
#define LOGW(a)  __android_log_write(ANDROID_LOG_WARN,LOG_TAG,a)
#define LOGFMTW(fmt,a)  __android_log_print(ANDROID_LOG_WARN,LOG_TAG,fmt,a)
#endif

int UUOO::getK(){
	return 9;
}

void UUOO::getAndChangeJavaField(JNIEnv *env, jobject testObj, jobject userObj){

	jclass user = env->GetObjectClass(userObj);
	//jfieldID GetFieldID(jclass clazz, const char* name, const char* sig)
	jfieldID ageId = env->GetFieldID(user,"age","I");
	//jint GetIntField(jobject obj, jfieldID fieldID)
	jint k = env->GetIntField(userObj,ageId);
	LOGFMTW("%d",k);

	jfieldID nameId = env->GetFieldID(user,"name","Ljava/lang/String;");
	jstring name = (jstring)(env->GetObjectField(userObj,nameId));
	jboolean *b = new unsigned char('0');
	const char* nameStr = env->GetStringUTFChars(name,b);
	LOGW(nameStr);
	env->ReleaseStringUTFChars(name,nameStr);

	//void SetIntField(jobject obj, jfieldID fieldID, jint value)
	env->SetIntField(userObj,ageId,18);

	name = env->NewStringUTF("我是一个小小鸟 ");
	env->SetObjectField(userObj,nameId,name);

}

void UUOO::callJavaMethod(JNIEnv *env, jobject testObj, jobject userObj){

	jclass user = env->GetObjectClass(userObj);
	jmethodID userGetUserNameId = env->GetMethodID(user,"getUserName","()Ljava/lang/String;");

	jstring jobj = (jstring)(env->CallObjectMethod(userObj,userGetUserNameId));
	jboolean *b = new unsigned char('0');
	const char* nameStr = env->GetStringUTFChars(jobj,b);
	LOGW(nameStr);
	env->ReleaseStringUTFChars(jobj,nameStr);


	jclass userStatic = env->FindClass("ndk/jni/User");
	jmethodID userGetUserNameId1 = env->GetStaticMethodID(userStatic,"getStaticUserName","(I)Ljava/lang/String;");
	LOGW("ppppppppp");
	jstring jobj1 = (jstring)(env->CallStaticObjectMethod(userStatic,userGetUserNameId1,4));
	const char* nameStr1 = env->GetStringUTFChars(jobj1,b);
	LOGW(nameStr1);
	env->ReleaseStringUTFChars(jobj1,nameStr1);

}

